package com.lagou.rocket;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * 并发测试工具类
 *
 * @author zyt
 * @create 2018-04-27 18:56
 **/
public class ConcurrentTestUtil {

    private static final int THREADNUM = 100;





    @SneakyThrows
    public static <T> void test(T t, Consumer<T> consumer, int threadNum){
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        for (int i=0;i<threadNum;i++){
            new Thread(()->{
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                consumer.accept(t);
            }).start();
            countDownLatch.countDown();
        }

    }

    public static <T> void test(T t,Consumer<T> consumer){
        test(t,consumer,THREADNUM);
    }

}

